package com.xdjd.mobile.business.service;

import com.gitee.easyopen.exception.ApiException;
import com.xdjd.mobile.business.dao.daoservice.member.MemberUserPoService;
import com.xdjd.mobile.business.dao.daoservice.member.MemberUserStepPoService;
import com.xdjd.mobile.business.dao.daoservice.member.MemberUserWalletPoService;
import com.xdjd.mobile.business.entity.model.member.MemberUserPo;
import com.xdjd.mobile.business.entity.model.member.MemberUserStepPo;
import com.xdjd.mobile.business.entity.model.member.MemberUserWalletPo;
import com.xdjd.mobile.business.entity.param.my.EditUserInfoParam;
import com.xdjd.mobile.business.entity.param.my.LoginParam;
import com.xdjd.mobile.business.entity.result.my.UserWalletResult;
import com.xdjd.mobile.business.util.ParamCheckUtils;
import com.xdjd.mobile.system.exception.IphoneException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2019/1/9 0009.
 */
@Service
public class MemberService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MemberUserWalletPoService memberUserWalletPoService;
    @Autowired
    private MemberUserStepPoService memberUserStepPoService;
    @Autowired
    private MemberUserPoService memberUserPoService;

    public MemberUserPo login(LoginParam loginParam) {
        if (null == loginParam || StringUtils.isEmpty(loginParam.getUserName())
                || StringUtils.isEmpty(loginParam.getLoginType()))
            throw new RuntimeException("请求参数为空");
        ParamCheckUtils.checkMobileNo(loginParam.getUserName(), "");
        if (StringUtils.equals(loginParam.getLoginType(), "2") && StringUtils.isEmpty(loginParam.getSmsCode()))
            throw new RuntimeException("短信验证码为空,请重新输入");
        if (StringUtils.equals(loginParam.getLoginType(), "1") && StringUtils.isEmpty(loginParam.getLoginPwd())) {
            throw new RuntimeException("密码不能为空");
        }
        return this.startLogin(loginParam);
    }

    private MemberUserPo startLogin(LoginParam loginParam) {
        MemberUserPo user = new MemberUserPo();
        if (StringUtils.equals(loginParam.getLoginType(), "1")) { // 密码登陆
            //userResp = passLogin(req, header);
        } else if (StringUtils.equals(loginParam.getLoginType(), "2")) { // 短信登录 或 短信注册
//            boolean isSuccess = loginService.checkVerifyCode(loginParam.getUserName(), loginParam.getSmsCode());
            boolean isSuccess=true;
            if (!isSuccess) {
                throw new ApiException("验证码错误", "-1");
            }
            user = loginService.isExistLoginUser(loginParam);
        } else {
            throw new RuntimeException("登陆方式错误");
        }
        return loginService.addUser2Redis(user);
    }

    public UserWalletResult findUserWalletInfo(Long memberId) {
        UserWalletResult result = new UserWalletResult();
        MemberUserWalletPo userWalletPo = memberUserWalletPoService.findWalletByMemberId(memberId);
        MemberUserStepPo userStepPo = memberUserStepPoService.findStepByMemberId(memberId);
        MemberUserPo member = memberUserPoService.getMemberById(memberId);
        result.setImage(member.getImage());
        result.setNickName(member.getNickName());
        result.setSignature(member.getSignature());
        result.setBalance(userWalletPo.getBalance());
        result.setTotalIncome(userWalletPo.getTotalIncome());
        result.setGoldCoins(userWalletPo.getGoldCoins());
        result.setFreezeFunds(userWalletPo.getFreezeFunds());
        BigDecimal totalStep = new BigDecimal(userStepPo.getTodayStepNumber()).add(new BigDecimal(userStepPo.getTotalStepNumber()));
        result.setTotalStep(totalStep.longValue());
        return result;
    }

    public void editUserInfo(EditUserInfoParam param, Long memberId) {
        MemberUserPo userPo = memberUserPoService.getMemberById(memberId);
        userPo.setImage(param.getUserLogo());
        userPo.setNickName(param.getNickName());
        userPo.setSignature(param.getSignature());
        memberUserPoService.updateMember(userPo);
    }
}
